package com.sangmo.boot.auth.core.handlers;

import com.sangmo.boot.auth.core.interfaces.GrantCodeRequest;
import com.sangmo.boot.auth.core.interfaces.GrantCodeResponse;
import com.sangmo.boot.auth.core.model.AuthorizeCode;
import com.sangmo.boot.auth.core.model.Client;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class BaseAuthorizeCodeHandler implements AuthorizeCodeHandler {
    @Autowired protected AuthorizeHelper authorizeHelper;

    protected abstract AuthorizeCode createCode(Client client, GrantCodeRequest req);

    @Override
    public GrantCodeResponse grant(GrantCodeRequest req) {
        Client client = authorizeHelper.validateClientGrant(req.getClientId(), req.getSecret(), Client.GrantTypes.CODE, req.getResource(), req.getScope());
        AuthorizeCode authorizeCode = this.createCode(client, req);

        GrantCodeResponse resp = new GrantCodeResponse();
        resp.setExpiry(authorizeCode.getLeftValidity());

        return resp;
    }
}
